package net.homeftp.heimo.weddingwidget.ui.colorpicker;

import net.homeftp.heimo.weddingwidget.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class ColorPickerDialog extends Dialog {
	private OnColorPickedListener mlistener = null;
	private int defaultColor = -1;
	private int defaultAlpha = 255;

	public ColorPickerDialog(Context context, OnColorPickedListener mlistener,
			int color,int alpha) {
		super(context);
		this.setTitle("Please pick a color");
		this.mlistener = mlistener;
		this.defaultColor = color;
		this.defaultAlpha = alpha;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.color_dialog_layout);

		GridView gridViewColors = (GridView) findViewById(R.id.gridViewColors);
		final ColorPickerAdapter adp = new ColorPickerAdapter(getContext(),defaultAlpha);
		gridViewColors.setAdapter(adp);
		
		int position = adp.getPosByColor(defaultColor);
		gridViewColors.setSelection(position);

		gridViewColors.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				int color = adp.getItemColorByPos(position);
				mlistener.colorPicked(color);
				ColorPickerDialog.this.dismiss();
			}
		});
	}
}
